package com.zm.dao.impl;

import com.zm.dao.IAccountDao;
import com.zm.domain.account;
import com.zm.utils.ConnectionUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.sql.SQLException;
import java.util.List;
@Repository("accountDao")
public class AccountDaoimpl implements IAccountDao {
    @Resource(name="runner")
    private QueryRunner runner;
    //QueryRunner中的连接由构造参数改为了方法参数注入
    private ConnectionUtil connectionUtil;
    public void setRunner(QueryRunner runner) {
        this.runner = runner;
    }
    public void setConnectionUtil(ConnectionUtil connectionUtil) {
        this.connectionUtil = connectionUtil;
    }
    public List<account> findAllAccount() {

        try {
            return runner.query( connectionUtil.getThreadConnection(),"select * from account"
                    , new BeanListHandler<account>(account.class));
        } catch (Exception e) {
            e.printStackTrace();
        }
    return null;
    }

    public List<account> findAllcountById(Integer id) {

        try {
            return runner.query(connectionUtil.getThreadConnection(),"select * from account where id=?"
                    ,new BeanListHandler<account>(account.class),id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public void saveAccount(account acc) {

    }

    public void updateAccount(account acc) {

        try {
            runner.update(connectionUtil.getThreadConnection(),"update account set money=? where id=?"
                    ,acc.getMoney(),acc.getId());
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


}
